package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.mapper.TMenuMapper;
import com.woniuxy.model.TMenu;
import com.woniuxy.service.TMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TMenuServiceImpl implements TMenuService {
    @Autowired
    private TMenuMapper tMenuMapper;
    @Override
    public void add(TMenu tMenu) {
        tMenu.setCreatetime(LocalDateTime.now());
        tMenu.setUpdatetime(LocalDateTime.now());
        tMenuMapper.insert(tMenu);
    }

    @Override
    public TMenu getById(Integer id) {
        return tMenuMapper.selectById(id);
    }

    @Override
    public void updateById(TMenu tMenu) {
        //如果不存在这样的id或者id为空则抛出异常
        if(tMenu.getMid()==null)
            throw new RuntimeException("id不能为空");
        TMenu tMenu1 = tMenuMapper.selectById((tMenu.getMid()));
        if(tMenu1==null)
            throw new RuntimeException("菜单不存在");
        tMenu.setUpdatetime(LocalDateTime.now());
        tMenuMapper.updateById(tMenu);
    }

    @Override
    public void deleteById(Integer mid) {
        tMenuMapper.deleteById(mid);
    }

    @Override
    public List<TMenu> page(Integer page, Integer pagesize, TMenu tMenu) {
        Page<TMenu> ipage = new Page<>(page,pagesize);
        QueryWrapper<TMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(tMenu.getMname()!=null&&tMenu.getMname()!="","mname",tMenu.getMname())
                .likeRight(tMenu.getIcon()!=null&&tMenu.getIcon()!="","icon",tMenu.getIcon());
        List<TMenu> tMenus = tMenuMapper.selectList(ipage, queryWrapper);
        return tMenus;
    }
}
